/**
 *
 */
package com.rf.richfitwheel.admin.sys.service.impl;

import com.rf.richfitwheel.admin.sys.dao.UserAuthDataMapper;
import com.rf.richfitwheel.admin.sys.model.UserAuthData;
import com.rf.richfitwheel.admin.sys.model.UserAuthDataExample;
import com.rf.richfitwheel.admin.sys.service.SysUserAuthDataService;
import com.rf.richfitwheel.common.contants.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhanglu
 *
 */
@Service("sysUserAuthDataService")
public class SysUserAuthDataServiceImpl implements SysUserAuthDataService {

    @Autowired
    private UserAuthDataMapper userAuthDataMapper;


    @Override
    public List<UserAuthData> getListByUserIdAndOrgId(String userId, String orgId) {
        UserAuthDataExample example = new UserAuthDataExample();
        UserAuthDataExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andOrgIdEqualTo(orgId);
        criteria.andOrgPropEqualTo("1");
        example.setOrderByClause(" data_index asc");
        List<UserAuthData> list = userAuthDataMapper.selectByExample(example);
        return list;
    }
    
    @Override
    public List<UserAuthData> getListByUserIdAndOrgId(String userId, String orgId, String moduleType) {
        UserAuthDataExample example = new UserAuthDataExample();
        UserAuthDataExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andOrgIdEqualTo(orgId);
        criteria.andOrgPropEqualTo("1");
        List<String> moduleTypeList = Arrays.stream(moduleType.split(",")).collect(Collectors.toList());
//        moduleTypeList.add(Constant.SystemModule.sys.name());
        criteria.andModuleTypeIn(moduleTypeList);
        example.setOrderByClause(" data_index asc");
        List<UserAuthData> list = userAuthDataMapper.selectByExample(example);
        return list;
    }
    @Override
    public Map<String, String> getAuthDataByUserIdAndOrgId(String userId, String orgId) {
        Map<String, String> dataLevels = new HashMap<>();
        if (userId == null || orgId == null) {
            return dataLevels;
        }
        List<UserAuthData> userDataLevels = this.getListByUserIdAndOrgId(userId, orgId);
        for (UserAuthData userDataLevel : userDataLevels) {
            dataLevels.put(userDataLevel.getModuleType(), userDataLevel.getDataLevel());
        }
        return dataLevels;
    }

    @Override
    public void deleteByUserIdAndOrgId(String userId, String orgId) {
        List<UserAuthData> userAuthDatas = this.getListByUserIdAndOrgId(userId, orgId);
        for (UserAuthData userAuthData : userAuthDatas) {
            userAuthDataMapper.deleteByPrimaryKey(userAuthData.getId());
        }
    }

    @Override
    public void deleteByUserId(String userId) {
        UserAuthDataExample example = new UserAuthDataExample();
        UserAuthDataExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<UserAuthData> userAuthDatas = userAuthDataMapper.selectByExample(example);
        for (UserAuthData userAuthData : userAuthDatas) {
            userAuthDataMapper.deleteByPrimaryKey(userAuthData.getId());
        }
    }
    
    /**
     * 只删除对应模块的记录
     *
     * @param userId     用户名
     * @param moduleType 模块
     */
    @Override
    public void deleteByUserIdAndModuleType(String userId, String moduleType){
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        List<String> modList = Arrays.stream(moduleType.split(",")).collect(Collectors.toList());
        modList.add(Constant.SystemModule.sys.name());
        params.put("moduleType", modList);
        userAuthDataMapper.deleteByUserIdAndModuleType(params);
    }
}
